package cn.xshi.rds.init;

import cn.xshi.rds.form.OMPRedisConfigForm;
import cn.xshi.rds.service.OMPRedisConfigService;
import cn.xshi.rds.model.OMPRedisConfig;
import cn.xshi.rds.util.JedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
 * @Desc Redis监控配置初始化（监控）
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Component
@Slf4j
@Order(0)
public class RedisConfigInit implements CommandLineRunner {

    @Resource
    JedisUtil jedisUtil;

    @Resource
    OMPRedisConfigService redisConfigService;

    @Override
    public void run(String... args) throws Exception {
        execute();
    }

    /**
     * 握手
     */
    public void execute() {
        new Thread(new RedisRunnable()).start();
    }

    /**
     * 采集日志
     */
    class RedisRunnable implements Runnable{
        public void run(){
            try {
                List<OMPRedisConfig> ompRedisConfigs = redisConfigService.getRedisConfigListByCondition(new OMPRedisConfigForm());
                for(OMPRedisConfig ompRedisConfig: ompRedisConfigs){
                    jedisUtil.initJedisPoolMap(ompRedisConfig);
                }
            }catch (Exception e){
                log.error("采集Reids配置初始化异常：{}",e);
            }
        }
    }
}
